import Vue from "vue";
import VueRouter from "vue-router";
import Cookies from 'js-cookie'

Vue.use(VueRouter)
Vue.use(Cookies)

const router = new VueRouter({
    routes: [
        {   //  主路由
            path: '/',
            redirect:'/home',
            component: () => import('@/views/Main.vue'),
            meta: {isAuth: true}, //  表示进入此组件需要登录
            children: [
                //  子路由
                { path: 'home', name: 'home', component: () => import('@/views/Home.vue'),meta: {isAuth: true}, },
                { path: 'user', name: 'user', component: () => import('@/views/User.vue'),meta: {isAuth: true}, },
                { path: 'productdetails', name: 'productdetails', component: () => import('@/views/ProductDetails.vue'),meta: {isAuth: true}, }
            ]
        },
        {path: '/login',component: () => import('@/views/Login.vue')}
    ]
})


//全局前置路由守卫————初始化的时候被调用、每次路由切换之前被调用
// router.beforeEach((to, from, next) => {
//     if (to.meta.isAuth) {
//         if (Cookies.get('userInfo')) {
//             next()  //放行
//         } else {
//             next('/login')
//         }
//     } else {
//         // 否则，放行
//         next()
//     }
// })

export default router